package com.axx.android.subject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.BaseModel;
import com.axx.android.common.Constants;
import com.axx.android.home.model.User;
import com.axx.android.letter.activity.RecipientActivity;
import com.axx.android.moments.ImageAdapter;
import com.axx.android.moments.ImageModel;
import com.axx.android.moments.ShowMeContentActivity;
import com.axx.android.profile.auth.activity.LoginActivity;
import com.axx.android.utils.GsonUtils;
import com.axx.android.utils.HttpUtils;
import com.axx.android.utils.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.previewlibrary.PhotoActivity;
import com.previewlibrary.ThumbViewInfo;

import org.json.JSONException;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SubjectCommentActivity extends BaseActivity {

    @Bind(R.id.btn_back)
    Button mBtnBack;
    @Bind(R.id.iv_image)
    ImageView mIvImage;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.tv_content)
    TextView mTvContent;
    @Bind(R.id.tv_time)
    TextView mTvTime;
    @Bind(R.id.tv_up_cnt)
    TextView mTvUpCnt;
    @Bind(R.id.activity_subject_comment)
    LinearLayout mActivitySubjectComment;
    @Bind(R.id.rv_image)
    RecyclerView rvImage;
    @Bind(R.id.iv_up)
    ImageView ivUp;

    @Override
    public int getLayoutId() {
        return R.layout.activity_subject_comment;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        ButterKnife.bind(this);

        final SubjectNow.SubjectDetailListBean model = (SubjectNow.SubjectDetailListBean) getIntent().getSerializableExtra("model");
        mTvTitle.setText(model.getUser().getNickName());
        mTvContent.setText(model.getText());
        mTvUpCnt.setText(model.getGood() + "");
        mTvTime.setText(getTime(model.getTime()));
        ImageLoader.loadRoundImage(model.getUser().getHeadPic(), mIvImage, this);

        if (model.getGoodStatus() == 1) {
            ivUp.setSelected(true);
        } else {
            ivUp.setSelected(false);
        }

        mIvImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SubjectCommentActivity.this,RecipientActivity.class);
                String json =  GsonUtils.toJson(model.getUser());
                try {
                    User.UserListBean userListBean = GsonUtils.fromJson(json,new TypeToken<User.UserListBean>(){});
                    intent.putExtra("model", (Serializable) userListBean);
                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        if (model.getPic() != null && model.getPic().size() != 0) {
            List<ImageModel> list = new ArrayList<>();
            final ArrayList<ThumbViewInfo> thumbViewInfos = new ArrayList<>();
            for (SubjectNow.SubjectDetailListBean.ImageBean imageBean : model.getPic()) {
                ImageModel imageModel = new ImageModel();
                imageModel.setAdd(false);
                imageModel.setCanDel(false);
                imageModel.setUrl(imageBean.getUrl());
                list.add(imageModel);

                ThumbViewInfo thumbViewInfo = new ThumbViewInfo(imageBean.getUrl());
                thumbViewInfos.add(thumbViewInfo);
            }

            ImageAdapter imageAdapter = new ImageAdapter(R.layout.view_item_image, list);
            rvImage.setLayoutManager(new GridLayoutManager(this, 4));
            rvImage.setAdapter(imageAdapter);

            imageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    PhotoActivity.startActivity(SubjectCommentActivity.this, thumbViewInfos, position);
                }
            });

        }

        ivUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (LetterApp.authUser == null){
                    startActivity(new Intent(SubjectCommentActivity.this, LoginActivity.class));
                    return;
                }
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("id", model.getId());
                hashMap.put("subjectId", model.getSubjectId());
                hashMap.put("userId",LetterApp.authUser.getId());
                HttpUtils.post(Constants.Url.Subject.SUBJECT_UP, hashMap, new HttpUtils.SJCallBack() {
                    @Override
                    public void onSuccess(String result) {
                        try {
                            BaseModel<Integer> stringBaseModel = GsonUtils.fromJson(result, new TypeToken<BaseModel<Integer>>() {
                            });
                            model.setGoodStatus(stringBaseModel.data);
                            if (stringBaseModel.data == 1){
                                ivUp.setSelected(true);
                                model.setGood(model.getGood() + 1);
                            }else {
                                ivUp.setSelected(false);
                                model.setGood(model.getGood() - 1);
                            }
                            mTvUpCnt.setText(model.getGood() + "");

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError() {

                    }
                });
            }
        });

    }
    private String getTime(String time){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);

            int year = calendar.get(Calendar.YEAR);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            calendar.setTime(new Date());
            int curYear = calendar.get(Calendar.YEAR);
            int curDay = calendar.get(Calendar.DAY_OF_MONTH);

            if (year == curYear && day == curDay){
                return new SimpleDateFormat("HH:mm").format(date);
            }else{
                return new SimpleDateFormat("MM-dd HH:mm").format(date);
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


}
